package base;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 测试JAVA-API层面，线程的6种状态
 */
@Slf4j(topic = "c.ThreadState")
public class TestState {
    public static void main(String[] args) throws InterruptedException {
        /*new*/
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                log.debug("running");
            }
        };
        /*runnable*/
        Thread t2 = new Thread("t2"){
            @Override
            public void run() {
                for(;;){}
            }
        };
        t2.start();

        /*terminated*/
        Thread t3 = new Thread("t3"){
            @Override
            public void run() {
                log.debug("running");
            }
        };
        t3.start();

        /*Timed_Waiting*/
        Thread t4 = new Thread("t4"){
            @SneakyThrows
            @Override
            public void run() {
                synchronized (TestState.class){
                    Thread.sleep(100000);
                }
            }
        };
        t4.start();

        /*Waiting*/
        Thread t5 = new Thread("t5"){
            @SneakyThrows
            @Override
            public void run() {
                t2.join();
            }
        };
        t5.start();

        /*Blocked*/
        Thread t6 = new Thread("t6"){
            @Override
            public void run() {
                synchronized (TestState.class){
                    log.debug("get a lock");
                }
            }
        };
        t6.start();

        TimeUnit.SECONDS.sleep(1);
        log.debug("t1 state:{}",t1.getState());
        log.debug("t2 state:{}",t2.getState());
        log.debug("t3 state:{}",t3.getState());
        log.debug("t4 state:{}",t4.getState());
        log.debug("t5 state:{}",t5.getState());
        log.debug("t6 state:{}",t6.getState());
    }

}
